package com.lmiot.xyclick.Util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import com.lmiot.xyclick.App.MyApp;
import com.youyi.yypermissionlibrary.SDK.YYPerUtils;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class ImgUtil {
    public static Bitmap bitmapPress(Bitmap bitmap, int i) {
        if (bitmap == null) {
            return bitmap;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, i, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    }

    public static void shareImg(String str) {
        if (TextUtils.isEmpty(str)) {
            ToastUtil.err("分享失败！");
            return;
        }
        Uri fromFile = Uri.fromFile(new File(str));
        if (Build.VERSION.SDK_INT >= 24) {
            Context context = MyApp.getContext();
            fromFile = FileProvider.getUriForFile(context, MyApp.getContext().getPackageName() + ".fileprovider", new File(str));
        }
        Intent intent = new Intent();
        intent.setAction("android.intent.action.SEND");
        intent.putExtra("android.intent.extra.STREAM", fromFile);
        intent.setType("image/*");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Intent createChooser = Intent.createChooser(intent, "分享到");
        createChooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MyApp.getContext().startActivity(createChooser);
    }

    public static void noticSystem(String str) {
        try {
            File file = new File(str);
            try {
                MediaStore.Images.Media.insertImage(MyApp.getContext().getContentResolver(), file.getAbsolutePath(), file.getName(), (String) null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Uri fromFile = Uri.fromFile(new File(str));
            if (Build.VERSION.SDK_INT >= 24) {
                Context context = MyApp.getContext();
                fromFile = FileProvider.getUriForFile(context, MyApp.getContext().getPackageName() + ".fileprovider", new File(str));
            }
            MyApp.getContext().sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", fromFile));
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    public static String convertIconToString(Bitmap bitmap) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            String encodeToString = Base64.encodeToString(byteArrayOutputStream.toByteArray(), 0);
            byteArrayOutputStream.close();
            return encodeToString;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static Bitmap zoomImg(Bitmap bitmap, int i) {
        float width = (float) bitmap.getWidth();
        Matrix matrix = new Matrix();
        float f = ((float) i) / width;
        matrix.postScale(f, f);
        return Bitmap.createBitmap(bitmap, 0, 0, (int) width, (int) ((float) bitmap.getHeight()), matrix, true);
    }

    public static String saveBitmpToAPPFileJPG(Bitmap bitmap, String str) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            File filesDir = MyApp.getContext().getFilesDir();
            File file = new File(filesDir, str + ".png");
            if (!file.exists()) {
                new File(file.getParent()).mkdirs();
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            byte[] bArr = new byte[10];
            ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
            while (true) {
                int read = byteArrayInputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream2.write(bArr, 0, read);
                } else {
                    fileOutputStream.write(byteArrayOutputStream2.toByteArray());
                    byteArrayOutputStream2.close();
                    byteArrayInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    return file.getAbsolutePath();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String saveBitmpToAPPFile(Bitmap bitmap, String str) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            File filesDir = MyApp.getContext().getFilesDir();
            File file = new File(filesDir, str + ".png");
            if (!file.exists()) {
                new File(file.getParent()).mkdirs();
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            byte[] bArr = new byte[10];
            ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
            while (true) {
                int read = byteArrayInputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream2.write(bArr, 0, read);
                } else {
                    fileOutputStream.write(byteArrayOutputStream2.toByteArray());
                    byteArrayOutputStream2.close();
                    byteArrayInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    return file.getAbsolutePath();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static Bitmap convertStringToIcon(String str) {
        try {
            byte[] decode = Base64.decode(str.replace("%%", "\n"), 0);
            return BitmapFactory.decodeByteArray(decode, 0, decode.length);
        } catch (Exception unused) {
            return null;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:18:0x0049  */
    /* JADX WARNING: Removed duplicated region for block: B:23:0x0051 A[SYNTHETIC, Splitter:B:23:0x0051] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static String imageToBase64(String r5) {
        /*
            boolean r0 = android.text.TextUtils.isEmpty(r5)
            r1 = 0
            if (r0 == 0) goto L_0x0008
            return r1
        L_0x0008:
            java.io.FileInputStream r0 = new java.io.FileInputStream     // Catch:{ IOException -> 0x0042, all -> 0x0040 }
            r0.<init>(r5)     // Catch:{ IOException -> 0x0042, all -> 0x0040 }
            int r5 = r0.available()     // Catch:{ IOException -> 0x003e }
            byte[] r5 = new byte[r5]     // Catch:{ IOException -> 0x003e }
            java.lang.String r2 = "ImageUtil"
            java.lang.StringBuilder r3 = new java.lang.StringBuilder     // Catch:{ IOException -> 0x003e }
            r3.<init>()     // Catch:{ IOException -> 0x003e }
            java.lang.String r4 = "imageToBase64===>"
            r3.append(r4)     // Catch:{ IOException -> 0x003e }
            int r4 = r0.available()     // Catch:{ IOException -> 0x003e }
            r3.append(r4)     // Catch:{ IOException -> 0x003e }
            java.lang.String r3 = r3.toString()     // Catch:{ IOException -> 0x003e }
            com.lmiot.xyclick.Util.LogUtil.d(r2, r3)     // Catch:{ IOException -> 0x003e }
            r0.read(r5)     // Catch:{ IOException -> 0x003e }
            r2 = 2
            java.lang.String r1 = android.util.Base64.encodeToString(r5, r2)     // Catch:{ IOException -> 0x003e }
            r0.close()     // Catch:{ IOException -> 0x0039 }
            goto L_0x004c
        L_0x0039:
            r5 = move-exception
            r5.printStackTrace()
            goto L_0x004c
        L_0x003e:
            r5 = move-exception
            goto L_0x0044
        L_0x0040:
            r5 = move-exception
            goto L_0x004f
        L_0x0042:
            r5 = move-exception
            r0 = r1
        L_0x0044:
            r5.printStackTrace()     // Catch:{ all -> 0x004d }
            if (r0 == 0) goto L_0x004c
            r0.close()
        L_0x004c:
            return r1
        L_0x004d:
            r5 = move-exception
            r1 = r0
        L_0x004f:
            if (r1 == 0) goto L_0x0059
            r1.close()     // Catch:{ IOException -> 0x0055 }
            goto L_0x0059
        L_0x0055:
            r0 = move-exception
            r0.printStackTrace()
        L_0x0059:
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: com.lmiot.xyclick.Util.ImgUtil.imageToBase64(java.lang.String):java.lang.String");
    }

    public static String getColorByXY(Bitmap bitmap, int i, int i2, boolean z) {
        if (bitmap != null) {
            try {
                int pixel = bitmap.getPixel(i, i2);
                String hexString = Integer.toHexString(Color.red(pixel));
                String hexString2 = Integer.toHexString(Color.green(pixel));
                String hexString3 = Integer.toHexString(Color.blue(pixel));
                if (hexString.length() == 1) {
                    hexString = "0" + hexString;
                }
                if (hexString2.length() == 1) {
                    hexString2 = "0" + hexString2;
                }
                if (hexString3.length() == 1) {
                    hexString3 = "0" + hexString3;
                }
                if (z) {
                    bitmap.recycle();
                }
                return (hexString + hexString2 + hexString3).toUpperCase();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static String saveBitmpToFile(Bitmap bitmap, String str) {
        if (!YYPerUtils.hasSD()) {
            return saveBitmpToCash(bitmap, str);
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            File file = new File(Environment.getExternalStorageDirectory() + "/" + "XYClick/temp");
            if (!file.exists()) {
                file.mkdir();
            }
            File file2 = new File(Environment.getExternalStorageDirectory() + "/" + "XYClick/temp", str + ".png");
            FileOutputStream fileOutputStream = new FileOutputStream(file2);
            byte[] bArr = new byte[10];
            ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
            while (true) {
                int read = byteArrayInputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream2.write(bArr, 0, read);
                } else {
                    fileOutputStream.write(byteArrayOutputStream2.toByteArray());
                    byteArrayOutputStream2.close();
                    byteArrayInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    return file2.getAbsolutePath();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String saveBitmpToCash(Bitmap bitmap, String str) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            File filesDir = MyApp.getContext().getFilesDir();
            File file = new File(filesDir, str + ".png");
            if (!file.exists()) {
                new File(file.getParent()).mkdirs();
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            byte[] bArr = new byte[10];
            ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
            while (true) {
                int read = byteArrayInputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream2.write(bArr, 0, read);
                } else {
                    fileOutputStream.write(byteArrayOutputStream2.toByteArray());
                    byteArrayOutputStream2.close();
                    byteArrayInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    return file.getAbsolutePath();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String saveBitmpToFile(Bitmap bitmap, File file) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            byte[] bArr = new byte[10];
            ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
            while (true) {
                int read = byteArrayInputStream.read(bArr);
                if (read != -1) {
                    byteArrayOutputStream2.write(bArr, 0, read);
                } else {
                    fileOutputStream.write(byteArrayOutputStream2.toByteArray());
                    byteArrayOutputStream2.close();
                    byteArrayInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    return file.getAbsolutePath();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static Bitmap viewToBitmap(View view) {
        view.buildDrawingCache();
        return view.getDrawingCache();
    }

    public static String saveRectImg(String str, Rect rect) {
        Bitmap decodeFile = BitmapFactory.decodeFile(str);
        Bitmap createBitmap = Bitmap.createBitmap(decodeFile, rect.left, rect.top, rect.width(), rect.height());
        Bitmap createBitmap2 = Bitmap.createBitmap(decodeFile.getWidth(), decodeFile.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap2);
        canvas.drawColor(-1);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(7.0f);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(-1);
        canvas.drawRect(new Rect(0, 0, createBitmap2.getWidth(), createBitmap2.getHeight()), paint);
        canvas.drawBitmap(createBitmap, (float) rect.left, (float) rect.top, (Paint) null);
        canvas.save();
        canvas.restore();
        return saveBitmpToFile(createBitmap2, "fullCut");
    }
}
